<?php

namespace app\forms;

use app\components\helpers\MappingHelper;
use app\components\validators\PositiveInteger;
use app\models\Contacts;
use app\models\Customer;
use yiier\validators\PhoneValidator;

/**
 * 客户联系人表单类
 */
class ContactForm extends Model
{
    public $id;
    public $customerId;
    public $name;
    public $sex;
    public $role;
    public $mobile;
    public $countryId;
    public $provinceId;
    public $cityId;
    public $districtId;
    public $street;
    public $description;

    public $page;
    public $pageSize;

    public function rules()
    {
        return [
            ['id', PositiveInteger::class],

            ['customerId', PositiveInteger::class],
            ['customerId', 'exist', 'targetClass' => Customer::class, 'targetAttribute' => 'id'],

            ['name', 'trim'],
            ['name', 'string', 'max' => 16],

            ['sex', 'in', 'range' => array_column(MappingHelper::sexList(), 'id')],
            ['sex', 'default', 'value' => MappingHelper::SEX_UNKNOWN],

            ['role', 'in', 'range' => array_column(MappingHelper::roleList(), 'id')],
            ['role', 'default', 'value' => ''],

            ['mobile', 'trim'],
            ['mobile', 'string', 'max' => 16],
            ['mobile', PhoneValidator::class],

            ['countryId', 'integer'],
            ['countryId', 'default', 'value' => 0],

            ['provinceId', 'integer'],
            ['provinceId', 'default', 'value' => 0],

            ['cityId', 'integer'],
            ['cityId', 'default', 'value' => 0],

            ['districtId', 'integer'],
            ['districtId', 'default', 'value' => 0],

            ['street', 'trim'],
            ['street', 'string', 'max' => 32],
            ['street', 'default', 'value' => ''],

            ['description', 'trim'],
            ['description', 'string', 'max' => 128],
            ['description', 'default', 'value' => ''],

            ['extJson', 'trim'],
            ['extJson', 'string', 'max' => 128],
            ['extJson', 'default', 'value' => ''],

            [['page', 'pageSize'], PositiveInteger::class],
            ['page', 'default', 'value' => 1],
            ['pageSize', 'default', 'value' => 10],

            ['customerId', 'required', 'on' => ['create', 'update']],
            ['id', 'exist', 'targetClass' => Contacts::class],
            ['id', 'required', 'on' => ['update', 'delete']],
            ['name', 'required', 'on' => ['create', 'update']],
            ['mobile', 'required', 'on' => ['create', 'update']],
        ];
    }

    public function scenarios()
    {
        return [
            'create' => [
                'customerId',
                'name',
                'sex',
                'role',
                'mobile',
                'countryId',
                'provinceId',
                'cityId',
                'districtId',
                'street',
                'description'
            ],
            'update' => [
                'id',
                'customerId',
                'name',
                'sex',
                'role',
                'mobile',
                'countryId',
                'provinceId',
                'cityId',
                'districtId',
                'street',
                'description'
            ],
            'index' => ['page', 'pageSize', 'name', 'customerId'],
            'delete' => ['id']
        ];
    }
}